using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Xml;

using FreeFlow.Client;

namespace FreeFlow.WinFormsControls
{
	/// <summary>
	/// Control to display a login form.
	/// </summary>
	public class LoginControl : ConnectableControl
	{
    private FreeFlow.WinFormsControls.FormControl formControl;
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

    /// <summary>
    /// Creates a new <see cref="LoginControl"/> instance.
    /// </summary>
		public LoginControl()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
      this.formControl = new FreeFlow.WinFormsControls.FormControl();
      this.SuspendLayout();
      // 
      // formControl
      // 
      this.formControl.AutoScroll = true;
      this.formControl.Dock = System.Windows.Forms.DockStyle.Fill;
      this.formControl.Location = new System.Drawing.Point(0, 0);
      this.formControl.Name = "formControl";
      this.formControl.Size = new System.Drawing.Size(304, 144);
      this.formControl.TabIndex = 0;
      this.formControl.SubmitForm += new System.EventHandler(this.formControl_CommitForm);
      this.formControl.CancelForm += new System.EventHandler(this.formControl_CancelForm);
      // 
      // LoginControl
      // 
      this.Controls.Add(this.formControl);
      this.Name = "LoginControl";
      this.Size = new System.Drawing.Size(304, 144);
      this.ResumeLayout(false);

    }
		#endregion

    private FreeFlow.Client.Form form;
    /// <summary>
    /// Populates the form.
    /// </summary>
    public void PopulateForm()
    {
      if (Connection != null)
      {
        form = Connection.LogOnForm();

        if (form != null)
        {
          formControl.Populate(form);
        }
      }
    }

    private Button GetButton(ButtonOperation operation)
    {
      for (int i = 0; i < formControl.Controls.Count; i++)
      {
        Control thisControl = formControl.Controls[i];
        Field field = form.Field(thisControl.Name);
        if (field != null)
        {
          if (field is ButtonField)
          {
            if (((ButtonField)field).Operation == operation)
              return (Button)thisControl;
          }
        }
      }

      return null;
    }

    /// <summary>
    /// Gets the button that will attempt to log the user in.
    /// </summary>
    public Button LoginButton
    {
      get
      {
        return GetButton(ButtonOperation.Submit);
      }
    }

    /// <summary>
    /// Gets the button that will cancel the login attempt.
    /// </summary>
    public Button CancelButton
    {
      get
      {
        return GetButton(ButtonOperation.Cancel);
      }
    }

    private void formControl_CommitForm(object sender, System.EventArgs e)
    {
      formControl.UpdateFormValues(form);
      if (formControl.ValidateFields())
      {
        try
        {
          Connection.LogOn(form);
          if (LoggedIn != null)
            LoggedIn(this, EventArgs.Empty);
        }
        catch(FreeFlowException ex)
        {
          MessageBox.Show(ex.Message);
        }
      }
    }

    /// <summary> 
    /// Event triggered when the user successfully logs in.
    /// </summary>
    [Category(Constants.FreeFlowCategory)]
    public event EventHandler LoggedIn;

    private void formControl_CancelForm(object sender, System.EventArgs e)
    {
      if (CancelledLogin != null)
        CancelledLogin(this, EventArgs.Empty);
    }

    /// <summary> 
    /// Event triggered when the user cancels a login.
    /// </summary>
    [Category(Constants.FreeFlowCategory)]
    public event EventHandler CancelledLogin;

    #region properties
    /// <summary>
    /// Gets the preferred height of the control.
    /// </summary>
    public int PreferredHeight
    {
      get
      {
        if (form != null)
          return form.Height;
        
        return Height;
      }
    }

    /// <summary>
    /// Gets the preferred width of the control.
    /// </summary>
    public int PreferredWidth
    {
      get
      {
        if (form != null)
          return form.Width;

        return Width; 
      }
    }

    /// <summary>
    /// Gets the preferred size of the control.
    /// </summary>
    public Size PreferredControlSize
    {
      get
      {
        return new Size(PreferredWidth, PreferredHeight);
      }
    }

    #endregion

	}
}
